package com.itheima.反射.反射获取类中字段;

import java.lang.reflect.Field;

/**
 * @Author 王加鑫
 * @Date 2021/5/26 15:29
 * @Version 1.0
 */
public class Demo {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        method1();

    }

    private static void method1() throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        //创建Student.class对象
        Class stuClass = Student.class;
        //获取class对象中所有的字段对象(包括私有的)
        Field field = stuClass.getDeclaredField("name");
        //暴力操作字段
        field.setAccessible(true);
        //反射创建对象
        Object o = stuClass.newInstance();
        //设置值
        field.set(o,"张三");
        //获取值
        System.out.println(field.get(o));
    }
}
